import React, { Component } from 'react';
import {
  AppRegistry,
  Dimensions,
  Platform,
  NativeModules,
  DeviceInfo,
  StyleSheet,
  Image,
  Text,
  View,
  TextInput,
  PixelRatio,
  TouchableOpacity,
  ScrollView,
  AsyncStorage,
  DeviceEventEmitter
} from 'react-native';
var Common = require('./common');
const pixelRatio = PixelRatio.get();
const dismissKeyboard = require('dismissKeyboard');
export default class EditInput extends Component<Props> {
  static navigationOptions = {
      headerTitle: '',
      tabBarLabel: '',

  };
  constructor(props){
    super(props);
    this.state = {
      str: this.props.navigation.state.params,
      text:''
    };
  }

  login(){
    var _this = this;
    Common.ajaxPost(Common.url + 'login.php', function(res){
      if(res.code == "0000"){

      }
    }.bind(this), {verify:'08login',t:''});
  }

  toRegister(){
    this.props.navigation.navigate('Register')
  }

  changeInfo(){
    var _this = this;

    var config = {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'credentials': 'include',
        'Cookie': _this.state.cookie
      },
      credentials: 'include'
    }
    var param = {}
    var str = this.state.str;
    var text = this.state.text.trim();
    if(!text){
      return;
    }
    if(str == '姓名'){
      param.realname = text;
    }else if(str == '身份证'){
      param.idcard = text;
    }else if(str == '公司名称'){
      param.companyname = text;
    }else if(str == '公司地址'){
      param.company_address = text;
    }else if(str == '联系方式'){
      param.mobile = text;
    }else if(str == '微信'){
      param.weixin = text;
    }
    if (param) {
      config["body"] = JSON.stringify(param);
    }
    console.log(param)
    fetch(Common.url + 'thinksns/index.php?app=public&mod=Account&act=doSaveProfile&is_ajax=1', config).then((response) => response.json()).then((res) => {
      console.log(res)
      if(res.status == 401){
        this.props.navigation.navigate('Login')
      }else if(res.status){
        switch(str){
          case '姓名': DeviceEventEmitter.emit('realname', text);//添加广播
                      break;
          case '身份证': DeviceEventEmitter.emit('idcard', text);//添加广播
                      break;
          case '公司名称': DeviceEventEmitter.emit('companyname', text);//添加广播
                      break;
          case '公司地址': DeviceEventEmitter.emit('address', text);//添加广播
                      break;
          case '联系方式': DeviceEventEmitter.emit('mobile', text);//添加广播
                      break;
          case '微信': DeviceEventEmitter.emit('weixin', text);//添加广播
                      break;

        }

        this.props.navigation.goBack();
      }
    })
  }


  componentDidMount(){
    AsyncStorage.getItem('cookie').then((value)=>{
			console.log(value)
			this.setState({
				cookie:value
			})
		})
    // console.log(this.props.navigation.state.params)
    // if(this.props.navigation.state.params.from){
    //   this.setState({
    //     from:this.props.navigation.state.params,
    //   })
    // }
  }
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <View style={styles.row}>
          <Text style={{fontSize:16,color:'#333',marginRight:20}}>{this.state.str}</Text>
          <TextInput
            style={styles.text}
            onChangeText={(text) => {
              this.setState({
                text: text
              })
            }}
            placeholder={'请输入'+this.state.str}
            placeholderTextColor='#d9d9d9'
          />
        </View>

        <TouchableOpacity style={styles.btnLogin} activeOpacity={0.6} onPress={this.changeInfo.bind(this)}>
          <Text style={{color:'#fff'}}>确定</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    height:Common.screenHeight,
    backgroundColor: '#fff',
  },
  row:{
    flexDirection:'row',
    alignItems:'center',
    paddingLeft:36,
    borderBottomWidth:1,
    borderBottomColor:'#f0f0f0'
  },
  text:{
    flex:1,
    height: 54,
    fontSize:18,
    color:'#333'
  },
  btnLogin:{
    height:36,
    backgroundColor:'#3e79fe',
    marginRight:38,
    marginLeft:38,
    marginTop:42,
    marginBottom:38,
    borderRadius:6,
    justifyContent:'center',
    alignItems:'center',
  },
  tips:{
    textAlign:'center',
    fontSize:14,
    color:'#a7a7a7'
  }
});
